package com.example.untitled1

import android.os.Build
import androidx.annotation.RequiresApi
import io.flutter.plugin.common.BinaryMessenger
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel



@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
class MethodChannelDemo(messenger: BinaryMessenger, private val bleManager: BleManager): MethodChannel.MethodCallHandler {

    private var channel: MethodChannel
    private var currentTimeMillis = 0L

    init {
        channel = MethodChannel(messenger, "com.example.ble_test.MethodChannel")
        channel.setMethodCallHandler(this)
//        val audio = AudioRecorder()
//        audio.start()
//        audio.registerMicrophoneCallback {
//            runBlocking {
//                launch(Dispatchers.Main) {
//                    channel.invokeMethod("audio", mapOf("data" to "data"+it.size.toString(),"time" to "time" + (System.currentTimeMillis()-currentTimeMillis).toString()) )
//                    currentTimeMillis = System.currentTimeMillis()
//                }
//            }
//        }
    }

    override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
        if (call.method == "sendData") {
            val byte:ByteArray? = call.argument<ByteArray>("data")
            result.success(mapOf("data" to "-------${System.currentTimeMillis()}"))
            byte?.let { bleManager.separate(it) }
        }
        if (call.method == "scan") {
            bleManager.scan(channel)
        }
        if(call.method == "connect"){
            val address = call.argument("address") as String?
            bleManager.connect(address)
        }
    }

}